#version 130

struct Material
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	float shininess;
	vec4 emission;
};

struct Light
{
	vec4 ambient;
	vec4 diffuse;
	vec4 specular;
	vec4 position;
	float range;
};

vec4 calc_phong_lighting (Light light, Material material, vec3 position, vec3 normal);

uniform Material material;
uniform Light light;
uniform int numLights;


uniform mat4 matrix_projection;
uniform mat4 matrix_world;
uniform mat4 matrix_view;

uniform mat4 matrix_bones[64];

in vec4 vertices;
in vec3 normals;
in vec4 tex_coords;
in ivec4  bones;
in vec4 bone_blends;

out vec2 tex_coord;
out vec4 color;

vec4 eval_skeleton_vector (ivec4 bones, vec4 blends, vec4 vec)
{
	return
		(matrix_bones[bones.x] * vec) * blends.x +
		(matrix_bones[bones.y] * vec) * blends.y +
		(matrix_bones[bones.z] * vec) * blends.z +
		(matrix_bones[bones.w] * vec) * blends.w;
}


void main(void)
{
  tex_coord = tex_coords.xy;

  vec3 normal = (matrix_world * eval_skeleton_vector (bones, bone_blends, vec4 (normals, 0.0))).xyz;
  vec4 position = matrix_world * eval_skeleton_vector (bones, bone_blends, vertices);

	color = calc_phong_lighting (light, material, position.xyz, normal);

  gl_Position = matrix_projection * matrix_view * position;
}
